﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FaWorks.WebSite.Controllers.Model
{
    public class Details2UserTypeController : Controller
    {
        #region CRUD
        [HttpGet, ActionName("CURD")]
        public ActionResult HttpGet(Int32 userID)
        {
            return this.Read(userID);
        }
        [HttpPost, ActionName("CURD")]
        public ActionResult HttpPost(Int32 userID, Int32 detailID, String userDetailsType)
        {
            return this.Create(userID, detailID, userDetailsType);
        }
        [HttpPut, ActionName("CURD")]
        public ActionResult HttpPut(Int32 id, Models.Knowledge.Details2UserType details2UserType)
        {
            return this.Update(id, details2UserType);
        }
        [HttpDelete, ActionName("CURD")]
        public ActionResult HttpDelete(Int32 id)
        {
            return this.Delete(id);
        }
        #endregion

        public ActionResult Read(Int32 userID)
        {
            return null;
        }

        public ActionResult Create(Int32 userID, Int32 detailID, String userDetailsType)
        {
            Models.AjaxResult ar = new Models.AjaxResult();
            try
            {
                KlDB.Database.ExecuteSqlCommand("DELETE tab " +
                    "FROM krUserKnowledgeDetailsType m " +
                        "INNER JOIN krDetails2UserType tab ON tab.krUserKnowledgeDetailsType_ID = m.ID " +
                    "WHERE m.[User_ID] = " + userID + " AND tab.krKnowledgeDetails_ID = " + detailID);

                if (!String.IsNullOrWhiteSpace(userDetailsType))
                {
                    foreach (var d2utID in userDetailsType.Split(','))
                    {
                        KlDB.Details2UserTypes.Add(new Models.Knowledge.Details2UserType
                        {
                            KnowledgeDetails_ID = detailID,
                            UserKnowledgeDetailsType_ID = Int32.Parse(d2utID)
                        });
                    }
                    KlDB.SaveChanges();
                }

                //Hubs.UserHub.Instance.Clients.All.Create(details2UserType);
            }
            catch (Exception ex)
            {
                ar.Success = false;
                ar.Data = ex.Message;
            }

            return Json(ar);
        }

        public ActionResult Update(Int32 id, Models.Knowledge.Details2UserType details2UserType)
        {
            return HttpNotFound();
        }

        public ActionResult Delete(Int32 id)
        {
            return HttpNotFound();
        }

        private KlDataBase KlDB = new KlDataBase();
    }
}
